﻿using System;
using System.Data.Entity;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TCM.DbContextt;
using TCM.Models;

namespace TCM.Forms
{
    public partial class TestPlanManager : Form
    {
        private BaseContext db;
        
        public TestPlanManager()
        {
            InitializeComponent();
            db = new BaseContext();
            db.Cases.Load();
            db.CasesToPlanses.Load();
            db.Categories.Load();
            db.TestPlans.Load();
            loadData();
        }

        private void loadData()
        {
            dataGridView1.DataSource = db.TestPlans.Local.ToBindingList();
        }


        private void refreshData()
        {
            dataGridView1.Refresh();

            for (int i = 0; i < dataGridView1.RowCount; i++)
            {
                var testPlanId = (Guid)dataGridView1.Rows[i].Cells[0].Value;
                dataGridView1.Rows[i].Cells[2].Value = db.CasesToPlanses.Where(x => x.TestPlanId == testPlanId).Count();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CategoriesManager testPlanManager = new CategoriesManager();
            var result = testPlanManager.ShowDialog(this);

            if (result == DialogResult.Cancel)
                return;

            TestPlan testPlan = new TestPlan();
            testPlan.TestplanId = Guid.NewGuid();
            testPlan.Name = testPlanManager.textBox1.Text;

            db.TestPlans.Add(testPlan);
            db.SaveChanges();
        }
    }
}
